package com.starblink.utils.stroage

import android.app.Application
import com.getkeepsafe.relinker.ReLinker
import com.tencent.mmkv.MMKV


object SKStorage {

    private lateinit var mmkv: MMKV

    fun initStorage(application: Application) {
        // fix:使用ReLinker修复java.lang.UnsatisfiedLinkError崩溃
        MMKV.initialize(application) { libName -> ReLinker.loadLibrary(application, libName); }
        //MMKV 需要使用多进程模式 来支持web进程
        mmkv = MMKV.defaultMMKV(MMKV.MULTI_PROCESS_MODE, null)
    }

    fun getMMMKV() = mmkv

    fun <T : Any> put(key: String, value: T): Unit {
        if (value is Int) {
            mmkv.putInt(key, value)
        }
        if (value is Float) {
            mmkv.putFloat(key, value)
        }
        if (value is ByteArray) {
            mmkv.putBytes(key, value)
        }
        if (value is Boolean) {
            mmkv.putBoolean(key, value)
        }
        if (value is Long) {
            mmkv.putLong(key, value)
        }
        if (value is String) {
            mmkv.putString(key, value)
        }
    }

    fun putStringSet(key: String, value: Set<String>) {
        mmkv.putStringSet(key, value)
    }

    fun getInt(key: String?, defValue: Int): Int {
        return mmkv.getInt(key, defValue)
    }

    fun getFloat(key: String?, defValue: Float): Float {
        return mmkv.getFloat(key, defValue)
    }

    fun getBoolean(key: String?, defValue: Boolean): Boolean {
        return mmkv.getBoolean(key, defValue)
    }

    fun getLong(key: String?, defValue: Long): Long {
        return mmkv.getLong(key, defValue)
    }

    fun getStringSet(key: String?, defValues: Set<String?>?): Set<String?>? {
        return mmkv.getStringSet(key, defValues)
    }

//    fun getMtStringSet(key: String?, defValues: Set<String>): MutableSet<String?>? {
//        return mmkv.getStringSet(key, defValues)
//    }

    fun getString(key: String?, defValue: String?): String? {
        return mmkv.getString(key, defValue)
    }

    fun getBytes(key: String?, defValue: ByteArray?): ByteArray? {
        return mmkv.getBytes(key, defValue)
    }

    fun remove(key: String) {
        mmkv.removeValueForKey(key)
    }

    fun removeKeys(keys: Array<String>) {
        mmkv.removeValuesForKeys(keys)
    }


}